//
//  CHSingleButtonView.swift
//  CHSimpleUI
//
//  Created by cyptt on 2023/4/20.
//

import UIKit
public enum CH_SINGLE_BUTTON_TITLE_LOCAL : Int {
    case left = 0  //左边
    case center = 1 //中间
    case right = 2 //中间

}
public class CHSingleOneWayButtonView: UIView {
    
    
    private var selfW:CGFloat = 0
    private var selfH:CGFloat = 0
    private var isShowView:Bool = false
    
    private var titleLocal:CH_SINGLE_BUTTON_TITLE_LOCAL = .center
    private var titleArr:[String] = []
    private var defaultColor:UIColor = UIColor.init(red: 67/255.0, green: 67/255.0, blue: 67/255.0, alpha: 1)
    private var selectedColor:UIColor = UIColor.init(red: 41/255.0, green: 199/255.0, blue: 172/255.0, alpha: 1)
    private var currentBtn:UIButton?
    private var clickItemCallBack:((_ index:Int,_ title:String?) -> ())?
    
    private let SigleChioseTAG:Int = 100000;
    
    private var buttonArr:[UIButton] = []
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.initViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit{
        print("CHSingleOneWayButtonView--deint")
    }
    
    public func setTitleArr(titleArr:[String],local:CH_SINGLE_BUTTON_TITLE_LOCAL  = .center,defaultColor:UIColor = UIColor.init(red: 67/255.0, green: 67/255.0, blue: 67/255.0, alpha: 1) ,selectedColor:UIColor =  UIColor.init(red: 41/255.0, green: 199/255.0, blue: 172/255.0, alpha: 1)){
        self.isShowView = true;
        self.titleArr = titleArr;
        
        self.defaultColor = defaultColor;
        self.selectedColor = selectedColor;
        
        self.titleLocal = local
        
    }
    private func initViews(){
        self.isShowView = false;
        selfW = self.frame.size.width;
        selfH = self.frame.size.height;
        
        
        
    }
    public func btnClickIndexBlockFun(clouse:@escaping(_ index:Int,_ title:String?)-> Void){
        self.clickItemCallBack = clouse
    }
    
 
    
    public func freshItem(index:Int,title:String){
        let btn:UIButton = self.buttonArr[index]
        btn.setTitle(title, for: .normal)
    }
           
    
    public func selectIndex(index:Int){
  
        let btn:UIButton = self.buttonArr[index]
       
        self.btnClick(btn: btn)
    
    }
      
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        selfW = self.frame.size.width;
        selfH = self.frame.size.height;
        //保证只加载一次
        if (self.isShowView == true) {
            showSelectView()
        }
        
        self.isShowView = false;
    }
    
    func showSelectView(){
        for i in 0..<self.titleArr.count {
            let btn:UIButton = UIButton.init(type: .custom)
            let x = (selfW*CGFloat(i))/CGFloat(self.titleArr.count)
            let w = selfW/CGFloat(self.titleArr.count)
            let h = selfH-10
            btn.frame = CGRect.init(x: x, y: 0, width: w, height: h)
            self .addSubview(btn)
            btn.setTitle(self.titleArr[i], for: .normal)
            btn.setTitleColor(self.defaultColor, for: .normal)
            btn.setTitleColor(self.selectedColor, for: .selected)
            
            if self.titleLocal == .left{
                btn.contentHorizontalAlignment = .left
            } else if self.titleLocal == .center{
                btn.contentHorizontalAlignment = .center
            }else if self.titleLocal == .right{
                btn.contentHorizontalAlignment = .right
            }
       
            if (i==0) {
                btn.isSelected = true;
                self.currentBtn = btn;
            }
            btn.tag = SigleChioseTAG + i
            btn.addTarget(self, action: #selector(btnClick(btn:)), for: .touchUpInside)
            
            self.buttonArr.append(btn)
            
        
        }
        
        self.btnIsCanUserClick()
    }
    
    
    func btnIsCanUserClick(){
     
        for i in 0..<self.buttonArr.count{
            let btn = self.buttonArr[i]
            let btnIndex = btn.tag - SigleChioseTAG
            let currentIndex = self.currentBtn!.tag - SigleChioseTAG
            if btnIndex > currentIndex{
                btn.isUserInteractionEnabled = false
            }else{
                btn.isUserInteractionEnabled = true
            }
        }
    }
    
}



extension CHSingleOneWayButtonView{
    @objc func btnClick(btn:UIButton){
        self.currentBtn?.isSelected = false;
        btn.isSelected = !btn.isSelected;
        self.currentBtn = btn;
        let index = btn.tag - SigleChioseTAG
 

        if (self.clickItemCallBack != nil) {
            self.clickItemCallBack!(index,btn.currentTitle);
        }
    }
}
